#!/bin/sh /etc/rc.common

START=99

RUNLOG_DIR=/tmp/hr

PROCESSED_REDIRECT=0

log()
{
	logger -t homeredirect $1
}

setupDefaultSrcIP() {
	if [ -z $src_ip ];then
		if [ "$1" = "ipv4" ]; then
			src_ip="0.0.0.0"
		else
			src_ip="::"
		fi
	fi
}

setup() {

	config_get enabled $1 enabled

	id=$1
	config_get proto $1 proto
	config_get src_ip $1 src_ip
	config_get src_dport $1 src_dport
	config_get dest_ip $1 dest_ip
	config_get dest_port $1 dest_port
	config_get name $1 name

	terminateRedirect $id
	
	[ "$enabled" != "1" ] && return 0

	PROCESSED_REDIRECT=1

	if [ "$proto" = "tcp4" ]; then
		src_addresstype="TCP4-LISTEN"
		dest_addresstype="TCP4"
		setupDefaultSrcIP "ipv4"
	elif [ "$proto" = "tcp6" ]; then
		src_addresstype="TCP6-LISTEN"
		dest_addresstype="TCP6"
		setupDefaultSrcIP "ipv6"
		src_ip="[$src_ip]"
		dest_ip="[$dest_ip]"
	elif [ "$proto" = "udp4" ]; then
		src_addresstype="UDP4-LISTEN"
		dest_addresstype="UDP4"
		setupDefaultSrcIP "ipv4"
	elif [ "$proto" = "udp6" ]; then
		src_addresstype="UDP6-LISTEN"
		dest_addresstype="UDP6"
		setupDefaultSrcIP "ipv6"
		src_ip="[$src_ip]"
		dest_ip="[$dest_ip]"
	fi

	#echo "nohup socat -lf $RUNLOG_DIR/$id.log $src_addresstype:$src_dport,bind=$src_ip,fork $dest_addresstype:$dest_ip:$dest_port > $RUNLOG_DIR/$id.log 2>&1 &"
	nohup socat -lf $RUNLOG_DIR/$id.log $src_addresstype:$src_dport,bind=$src_ip,fork $dest_addresstype:$dest_ip:$dest_port > $RUNLOG_DIR/$id.log 2>&1 &
	log "[HomeRedirect] Port redirect from $proto $src_ip:$src_dport==>$dest_addresstype:$dest_ip:$dest_port started."
}

# param $1 is port
showTcpPortState() {
	local process=$(netstat -ltnp | awk -F ' ' '{if(NR>2) print $1"/"$4"/"$7}' | grep :$1)
	if [ -n "$process" ]; then
		echo $process
	else
		echo 'TCP Port $1 is Free.'
	fi
}
# param $1 is port
showUdpPortState() {
	local process=$(netstat -lunp | awk -F ' ' '{if(NR>2) print $1"/"$4"/"$6}'|grep :$1)
	if [ -n "$process" ]; then
		echo $process
	else
		echo 'UDP Port $1 is Free.'
	fi
}

isRedirectRunning() {
	local runningPID=$(ps | grep socat | grep $RUNLOG_DIR/$1 | sed '/grep/d' | awk -F ' ' '{print $1}')
	if [ -n "$runningPID" ]; then
		return 1
	else
		return 0
	fi
}

# param $1 is redirect id
terminateRedirect() {
	isRedirectRunning $1
	[ "$?" = "1" ] && {
		local runningPID=$(ps | grep socat | grep $RUNLOG_DIR/$1 | sed '/grep/d' | awk -F ' ' '{print $1}')
		#echo "Going to kill process $runningPID"
		kill $runningPID
	}
}

terminateAll() {
	local runningPIDs=$(ps | grep socat | grep $RUNLOG_DIR | sed '/grep/d' | awk -F ' ' '{print $1}')
	[ -n "$runningPIDs" ] && {
		kill $runningPIDs
		log "Redirect process : $runningPIDs stopped."
	}
}

start() {
	local vt_enabled=$(uci -q get homeredirect.@global[0].enabled)
	if [ "$vt_enabled" = 0 ]; then
		terminateAll
		fw3 reload
		return 1
	fi
	
	rm -rf $RUNLOG_DIR
	mkdir -p $RUNLOG_DIR
	
	config_load homeredirect
	
	PROCESSED_REDIRECT=0
	config_foreach setup redirect
	[ "$PROCESSED_REDIRECT" == "1" ] && {
		fw3 reload
	}
	log 'HomeRedirect started.'
}

stop() {
	terminateAll
	fw3 reload
	log 'HomeRedirect stopped.'
}
